<template>
  <div class="treatmentinSide">
    <div class="nav-search">
      <el-input v-model="searchParams.serachObj" placeholder="患者姓名/医生姓名/诊所名称/订单编号/病例编号"></el-input>
      <el-date-picker
        v-model="searchParams.createDate"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="创建日期"
      >
      </el-date-picker>
      <span>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-download" @click="exportList">导出</el-button>
      </span>
      <div class="tableHandle">
        <!-- 高级搜索 -->
        <lm-popver @queryAdvanced="toQuery" @resetAdvanced="resetAdvanced">
          <p class="searchForm">
            <el-input v-model="searchParams.name" placeholder="患者姓名"></el-input>
          </p>
          <p class="searchForm">
            <el-input v-model="searchParams.id" placeholder="患者编号"></el-input>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.orderTreatStage" placeholder="诊疗阶段">
              <el-option
                v-for="item in dict.order_treat_stage"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.recordStat" placeholder="诊疗状态">
              <el-option
                v-for="item in dict.record_stat"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.gender" placeholder="患者性别">
              <el-option
                v-for="item in dict.gender"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.orderSubStat" placeholder="订单状态">
              <el-option
                v-for="item in dict.order_sub_stat"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.subOrderTreatStage" placeholder="订单类型">
              <el-option
                v-for="item in dict.sub_order_treat_stage"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.expressStat" placeholder="物流状态">
              <el-option
                v-for="item in dict.express_stat"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.package" placeholder="选择套餐">
              <el-option
                v-for="item in packAge"
                :key="item.id"
                :label="item.packageName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.product" placeholder="选择产品">
              <el-option
                v-for="item in productAll"
                :key="item.id"
                :label="item.productName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.provinceId" placeholder="诊所所在省" @change="provinceChange">
              <el-option
                v-for="item in province"
                :key="item.areaId"
                :label="item.name"
                :value="item.areaId"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-select v-model="searchParams.cityId" placeholder="诊所所在市">
              <el-option
                v-for="item in city"
                :key="item.areaId"
                :label="item.name"
                :value="item.areaId"
              >
              </el-option>
            </el-select>
          </p>
          <p class="searchForm">
            <el-date-picker
              v-model="searchParams.createDate"
              :clearable="false"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="创建日期"
              style="width:186px"
            >
            </el-date-picker>
          </p>
          <p class="searchForm">
            <el-input v-model="searchParams.salesId" placeholder="所属销售"></el-input>
          </p>
        </lm-popver>
        <lm-operation></lm-operation>
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="data"
        size="small"
        style="width: 100%;"
        @selection-change="selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="订单编号" width="150"></el-table-column>
        <el-table-column prop="orderSubStat" label="订单状态">
          <template slot-scope="scope">
            {{ dict.label.order_sub_stat[scope.row.orderSubStat] }}
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="确认制作时间" width="150"></el-table-column>
        <el-table-column prop="subOrderTreatStage" label="订单类型"></el-table-column>
        <el-table-column prop="orderPayFlag" label="费用类型">
          <template slot-scope="scope">
            {{ dict.label.order_pay_flag[String( scope.row.orderPayFlag )] }}
          </template>
        </el-table-column>
        <el-table-column prop="patientName" label="患者姓名"></el-table-column>
        <el-table-column prop="doctorName" label="主治医生"></el-table-column>
        <el-table-column prop="patientId" label="病例编号" width="150"></el-table-column>
        <el-table-column prop="packageName" label="套餐名称"></el-table-column>
        <el-table-column prop="productName" label="产品名称"></el-table-column>
        <el-table-column prop="clinicName" label="诊所名称" width="150"></el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.pageSize"
        :total="total"
        :current-page.sync="page.pageIndex"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <el-dialog
      title="导出"
      :visible.sync="dialogVisible"
      width="35%"
      :append-to-body="true"
      :before-close="handleClose"
    >
      <div class="dialog-header">
        <span>导出字段</span>
        <span>
          <el-button class="filter-item" size="mini" type="warning" @click="unCheckAll">清空</el-button>
          <el-button class="filter-item" size="mini" type="success" @click="checkAll">全选</el-button>
        </span>
      </div>
      <div class="dialog-content">
        <el-checkbox-group v-model="checkedMenu">
          <el-checkbox v-for="item in menuList" :key="item.value" :label="item.value">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <p class="dialog-fotter">
          <el-button class="filter-item" size="mini" type="success" @click="exportData">导出</el-button>
        </p>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { query, operPackage, operProduct, saleArea, download } from '@/api/order';
import lmPopver from '@/views/order/compontents/lm-popver';
import lmOperation from '@/views/order/compontents/lm-operation';

export default {
  name: 'TreatmentInside',
  components: { lmPopver, lmOperation },
  dicts: ['order_treat_stage', 'order_sub_stat', 'express_stat', 'sub_order_treat_stage', 'order_pay_flag', 'gender', 'record_stat'],
  data() {
    return {
      loading: false,
      searchParams: {
        serachObj: null, // 患者姓名/医生姓名/诊所名称/订单编号/病例编号
        createDate: null, // 创建时间
        name: null, // 患者姓名
        id: null, // 患者编号
        orderTreatStage: null, // 诊疗阶段
        gender: null, // 患者性别
        orderStat: null, // 订单状态
        subOrderTreatStage: null, // 订单类型
        expressStat: null, // 物流状态
        package: null, // 套餐
        product: null, // 产品
        provinceId: null, // 省id
        cityId: null, // 市id
        salesId: null // 所属销售
      },
      data: [],
      packAge: [], // 套餐
      productAll: [], // 产品
      province: [], // 省
      city: [], // 市
      page: {
        pageSize: 10,
        pageIndex: 1
      },
      total: 0,
      dialogVisible: false,
      checkedMenu: ['id', 'orderSubStat', 'createDate', 'subOrderTreatStage',
        'patientName', 'doctorName', 'patientId', 'packageName', 'productName', 'clinicName'],
      menuList: [
        { name: '订单编号', value: 'id' },
        { name: '订单状态', value: 'orderSubStat' },
        { name: '确认制作时间', value: 'createDate' },
        { name: '费用类型', value: 'subOrderTreatStage' },
        { name: '患者姓名', value: 'patientName' },
        { name: '主治医生', value: 'doctorName' },
        { name: '病例编号', value: 'patientId' },
        { name: '套餐名称', value: 'packageName' },
        { name: '产品名称', value: 'productName' },
        { name: '诊所名称', value: 'clinicName' }
      ]
    };
  },
  computed: {},
  mounted() {
    this.queryOrderList({ pageIndex: 0, pageSize: 10 });
    operPackage().then(res => {
      this.packAge = res;
    }).catch(er => {});
    operProduct().then(res => {
      this.productAll = res;
    }).catch(er => {});
    saleArea({ parentAreaId: '-1', sonlevel: '1' }).then(res => {
      this.province = res;
    }).catch(er => {});
  },
  methods: {
    selectionChangeHandler() {},
    // 搜索
    toQuery() {
      const search = {
        pageIndex: this.page.pageIndex - 1,
        pageSize: this.page.pageSize,
        ...this.searchParams
      };
      this.queryOrderList(search);
    },
    // 重置
    resetAdvanced() {
      for (const key in this.searchParams) {
        this.searchParams[key] = null;
      }
    },
    // 关闭导出
    handleClose() {
      this.dialogVisible = false;
    },
    // 导出
    exportList() {
      this.dialogVisible = true;
    },
    // 全选
    checkAll() {
      this.checkedMenu = this.menuList.map(item => item.value);
    },
    // 清空
    unCheckAll() {
      this.checkedMenu = [];
    },
    // 导出
    exportData() {
      const params = {
        ...this.searchParams
      };
      download('/order/query/download', params).then(res => {
        const url = window.URL.createObjectURL(res);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.href = url;
        a.download = '订单数据.xlsx';
        a.click();
        window.URL.revokeObjectURL(url);
      }).catch(er => {});
      this.handleClose();
    },
    sizeChangeHandler(item) {
      this.toQuery();
    },
    pageChangeHandler(item) {
      this.toQuery();
    },
    // 省切换
    provinceChange(parentAreaId) {
      this.city = [];
      this.searchParams.cityId = null;
      saleArea({ parentAreaId, sonlevel: '2' }).then(res => {
        this.city = res;
      });
    },
    // 列表
    queryOrderList(params) {
      this.loading = true;
      query(params).then(res => {
        this.data = res.content || [];
        this.total = res.totalElements || 0;
        this.loading = false;
      }).catch(er => {
        this.loading = false;
      });
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search{
  padding: 10px 0;
  .el-input{
    width: 320px;
    margin-right: 10px;
  }
  .el-date-editor--date{
    width: 150px;
  }
}
.tableHandle{
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
.searchForm{
  width: 49%;
  display: inline-block;
  margin: 5px 0px;
}
.el-checkbox{
  margin: 0;
  margin-left: 10%;
  width: 40%;
  height: 35px;
  line-height: 35px;
}
.dialog-header{
  span:nth-child(1){
    font-weight: 800;
    font-size: 16px;
    margin-left: 10%;
    margin-right: 20px;
  }
}
.dialog-fotter{
  margin: 0 auto;
  text-align: center;
}
</style>
<style>
.el-dialog .el-dialog__body{
  padding: 10px;
}
</style>
